package com.rudolfheszele.smsselector.controller;

import com.rudolfheszele.smsselector.R;
import com.rudolfheszele.smsselector.SmsSelectorMainActivity;
import com.rudolfheszele.smsselector.model.SmsSelectorRuleSet;
import com.rudolfheszele.smsselector.model.SmsSelectorRuleSetException;
import com.rudolfheszele.smsselector.view.SmsSelectorSmsViewerActivity;
import com.rudolfheszele.smsselector.view.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class is used to update the progress in the progress bar while reading the messages
 * @author erudhes
 * @see Runnable
 * @version 0.2
 */
class SmsSelectorMainActivityProgressBarUpdater implements Runnable
{
	public SmsSelectorMainActivityProgressBarUpdater(SmsSelectorMainActivity mainActivity, int percent)
	{
		mPercent = percent;
		mMainActivity = mainActivity;
	}
	
	@Override
	public void run()
	{
		ProgressBar progressBar = (ProgressBar)mMainActivity.findViewById(R.id.main_activity_progress_bar_id);
		
		progressBar.setProgress(mPercent);
	}
	
	private int mPercent;
	private SmsSelectorMainActivity mMainActivity;
}

/**
 * This class is used to show the folder the application reading from to the user
 * @author erudhes
 * @see Runnable
 * @version 0.2
 */
class SmsSelectorMainActivityTextViewUpdater implements Runnable
{
	public SmsSelectorMainActivityTextViewUpdater(SmsSelectorMainActivity mainActivity, String folder)
	{
		mMainActivity = mainActivity;
		mFolder = folder;
	}
	
	@Override
	public void run()
	{
		TextView textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);
		
		textView.setText(mMainActivity.getString(R.string.main_activity_text_view_reading_text) + "(from " + mFolder + ")");
	}
	
	private SmsSelectorMainActivity mMainActivity;
	private String mFolder;
}

/**
 * This class is used to inform the user about the finish of the messages reading
 * @author erudhes
 * @see Runnable
 * @version 0.2
 */
class SmsSelectorMainActivityFinishUpdater implements Runnable
{
	public SmsSelectorMainActivityFinishUpdater(SmsSelectorMainActivity mainActivity)
	{
		mMainController = (SmsSelectorMainController)ControllerFactoryFactory.getControllerFactory().getController();
		mMainActivity = mainActivity;
	}

	@Override
	public void run()
	{	
		TextView textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);
		Button startButton = (Button)mMainActivity.findViewById(R.id.main_activity_start_button_id);
		
		mMainController.stopSmsReaderService();
		textView.setText(mMainActivity.getString(R.string.main_activity_text_view_finished_text));
		startButton.setText(mMainActivity.getString(R.string.main_activity_start_button_proceed_text));
	}
	
	SmsSelectorMainController mMainController;
	SmsSelectorMainActivity mMainActivity;
}

/**
 * This class is responsible to control the Main Activity of the application
 * @author erudhes
 * @version 0.2
 */
public class SmsSelectorMainActivityController implements SmsReaderController, OnClickListener
{
	/**
	 * The constructor. Receives the main activity of the application
	 * @param activity The main activity of the application
	 */
	public SmsSelectorMainActivityController(Activity activity)
	{
		mMainActivity = (SmsSelectorMainActivity)activity;
		mMainController = (SmsSelectorMainController)ControllerFactoryFactory.getControllerFactory().getController();
		mSmsReaderService = null;
		mSmsViewerActivityIntent = null;
		registerMainActivity();
	}
	
	/**
	 * Called when a button is clicked anywhere on the main activity
	 * @param view The Android view the click event happened on
	 * @see OnClickListener
	 */
	@Override
	public void onClick(android.view.View view)
	{
		switch(view.getId())
		{
			case R.id.main_activity_cancel_button_id:
				onMainActivityCancelButtonClick(view);
				break;
			case R.id.main_activity_pause_button_id:
				onMainActivityPauseButtonClick(view);
				break;
			case R.id.main_activity_start_button_id:
				onMainActivityStartButtonClick(view);
				break;
		}
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public View getView(int viewId)
	{
		return mMainActivity;
	}

	/**
	 * @see Controller
	 */
	@Override
	public void registerView(View view)
	{
		mMainActivity = (SmsSelectorMainActivity)(view);
		registerMainActivity();
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public void updateView(View view)
	{
		// Nothing to do yet...
	}

	/**
	 * @see Controller
	 */
	@Override
	public void updateGui(int viewId, Runnable task)
	{
		mMainActivity.runOnUiThread(task);
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public boolean buildMenu(View view, Object menu, Object clicked)
	{
		// There is no menu on the main activity
		return true;
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public boolean menuClicked(View view, Object menu)
	{
		// There is no menu on the main activity
		return true;
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public void saveState(int viewId, Object state)
	{
		Bundle bundle = (Bundle)state;
		ProgressBar progressBar = (ProgressBar)mMainActivity.findViewById(R.id.main_activity_progress_bar_id);
		TextView textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);
		Button startButton = (Button)mMainActivity.findViewById(R.id.main_activity_start_button_id);
		Button pauseButton = (Button)mMainActivity.findViewById(R.id.main_activity_pause_button_id);
		int progress = 0;
		String text = "";
		String start = "";
		String pause = "";
		
		if(progressBar != null)
			progress = progressBar.getProgress();
		if(textView != null)
			text = textView.getText().toString();
		if(startButton != null)
			start = startButton.getText().toString();
		if(pauseButton != null)
			pause = pauseButton.getText().toString();
		bundle.putInt("Progress", progress);
		bundle.putString("Text", text);
		bundle.putString("Start", start);
		bundle.putString("Pause", pause);
		if(mSmsReaderService != null)
			mSmsReaderService.pauseSmsReading();
	}
	
	/**
	 * @see Controller
	 */
	@Override
	public void restoreState(int viewId, Object state)
	{
		Bundle bundle = (Bundle)state;
		ProgressBar progressBar = (ProgressBar)mMainActivity.findViewById(R.id.main_activity_progress_bar_id);
		TextView textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);
		Button startButton = (Button)mMainActivity.findViewById(R.id.main_activity_start_button_id);
		Button pauseButton = (Button)mMainActivity.findViewById(R.id.main_activity_pause_button_id);
		
		if(progressBar != null)
			progressBar.setProgress(bundle.getInt("Progress"));
		if(textView != null)
			textView.setText(bundle.getString("Text"));
		if(startButton != null)
			startButton.setText(bundle.getString("Start"));
		if(pauseButton != null)
			pauseButton.setText(bundle.getString("Pause"));
		if(mSmsReaderService != null)
			mSmsReaderService.resumeSmsReading();
	}
	
	/**
	 * @see SmsReaderController
	 */
	@Override
	public void smsReadingStarted(SmsSelectorSmsReaderService smsReaderService)
	{
		mSmsReaderService = smsReaderService;
	}
	
	/**
	 * @see SmsReaderController
	 */
	@Override
	public void smsReadingFinished()
	{
		updateGui(0, new SmsSelectorMainActivityFinishUpdater(mMainActivity));
		mSmsReaderService = null;
	}
	
	/**
	 * @see SmsReaderController
	 */
	@Override
	public void reportProgress(ProgressState progressState, Object additionalInfo)
	{
		String directory = null;
		int progress = 0;
		
		switch(progressState)
		{
			case DIRECTORY_CHANGED_STATE:
				directory = (String)additionalInfo;
				updateGui(0, new SmsSelectorMainActivityTextViewUpdater(mMainActivity, directory));
				break;
			case NEW_PROGRESS_STATE:
				progress = ((Integer)additionalInfo).intValue();
				updateGui(0, new SmsSelectorMainActivityProgressBarUpdater(mMainActivity, progress));
				break;
		}
	}
	
	/**
	 * This method registers every needed event handlers on the main activity
	 */
	private void registerMainActivity()
	{
		// The main activity just registers, read the rules from the database if needed
		if(mMainController.getRuleSets() == null)
		{
			try
			{
				mMainController.setRuleSets(SmsSelectorRuleSet.readRuleSetsFromDatabase(mMainActivity));
			}
			catch(SmsSelectorRuleSetException exception)
			{
				Toast.makeText(mMainActivity, "Could not fetch rulesets from database!!!", 10);
			}
		}
		else if(mMainController.getSmsMessages() != null)
		{
			// Everything is already read, we can move forward
			mSmsViewerActivityIntent = new Intent(mMainActivity, SmsSelectorSmsViewerActivity.class);
			mMainActivity.startActivity(mSmsViewerActivityIntent);
		}
		mMainActivity.findViewById(R.id.main_activity_cancel_button_id).setOnClickListener(this);
		mMainActivity.findViewById(R.id.main_activity_pause_button_id).setOnClickListener(this);
		mMainActivity.findViewById(R.id.main_activity_start_button_id).setOnClickListener(this);
	}
	
	/**
	 * This method is called, when the Cancel button is clicked on the main activity
	 * @param view The cancel button itself
	 */
	private void onMainActivityCancelButtonClick(android.view.View view)
	{
		Button pauseButton = null;
		TextView textView = null;
		String pauseString = null;
		String textViewStartString = null;
		
		// If the messages reading has not been started we can just quit from the application
		if(mSmsReaderService == null)
		{
			mMainActivity.finish();
			return;
		}
		// The messages reading has been started, so cancel it
		mSmsReaderService.cancelSmsReading();
		pauseString = mMainActivity.getString(R.string.main_activity_pause_button_text_pause);
		textViewStartString = mMainActivity.getString(R.string.main_activity_text_view_start_text);
		pauseButton = (Button)mMainActivity.findViewById(R.id.main_activity_pause_button_id);
		textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);
		// Set the texts of the buttons according to the new state...
		pauseButton.setText(pauseString);
		textView.setText(textViewStartString);
		mMainController.stopSmsReaderService();
	}
	
	/**
	 * This method is called, when the Pause button is clicked on the main activity
	 * @param view The pause button itself
	 */
	private void onMainActivityPauseButtonClick(android.view.View view)
	{
		Button pauseButton = null;
		String pauseString = null;
		String resumeString = null;
		
		// The service has not been started, there is nothing to pause
		if(mSmsReaderService == null)
			return;
		pauseButton = (Button)view;		
		pauseString = mMainActivity.getString(R.string.main_activity_pause_button_text_pause);
		resumeString = mMainActivity.getString(R.string.main_activity_pause_button_text_resume);
		
		// The text of the button determines what needs to be done
		if(pauseButton.getText().equals(pauseString))
		{
			mSmsReaderService.pauseSmsReading();
			pauseButton.setText(resumeString);
		}
		else if(pauseButton.getText().equals(resumeString))
		{
			mSmsReaderService.resumeSmsReading();
			pauseButton.setText(pauseString);
		}
	}
	
	/**
	 * This method is called, when the Start button is clicked on the main activity
	 * @param view The start button itself
	 */
	private void onMainActivityStartButtonClick(android.view.View view)
	{
		Button startButton = (Button)mMainActivity.findViewById(R.id.main_activity_start_button_id);
		String startString = mMainActivity.getString(R.string.main_activity_start_button_start_text);
		String proceedString = mMainActivity.getString(R.string.main_activity_start_button_proceed_text);
		TextView textView = (TextView)mMainActivity.findViewById(R.id.main_activity_text_view_id);

		if(startButton.getText().equals(startString) && textView.getText().equals(mMainActivity.getString(R.string.main_activity_text_view_start_text)))
		{
			// We can start the messages reading...
			mMainController.startSmsReaderService(this);
			textView.setText(mMainActivity.getString(R.string.main_activity_text_view_reading_text));
		}
		else if(startButton.getText().equals(proceedString))
		{
			// The SMS reading has been finished, let's proceed to the next view
			mSmsViewerActivityIntent = new Intent(mMainActivity, SmsSelectorSmsViewerActivity.class);
			mMainActivity.startActivity(mSmsViewerActivityIntent);
		}
	}
	
	private SmsSelectorMainController mMainController;
	private SmsSelectorMainActivity mMainActivity;
	private SmsSelectorSmsReaderService mSmsReaderService;
	private Intent mSmsViewerActivityIntent;
}
